﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using ARR.Entities;
using ARR.UI.ViewModels;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for MissingArticlesPopup.xaml
    /// </summary>
    public partial class MissingArticlesPopupView : Window
    {
        public MissingArticlesPopupView()
        {
            InitializeComponent();
            ArticlesGV.PreparingCellForEdit += new EventHandler<GridViewPreparingCellForEditEventArgs>(ArticlesGV_PreparingCellForEdit);
        }

        void ArticlesGV_PreparingCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "Category")
            {
                MissingArticlesPopupViewModel model = this.DataContext as MissingArticlesPopupViewModel;
                sourceCategoryRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = sourceCategoryRadDropDownButton.DropDownContent as RadTreeView;
                if (treeview != null)
                {
                    treeview.ItemsSource = model.Categories;
                    sourceCategoryItem = e.Row.DataContext;
                }
            }
        }


        private object sourceCategoryItem;
        private RadDropDownButton sourceCategoryRadDropDownButton;
        private void SourceCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;

            if (((Category)treeview.SelectedItem).Level == 2)
            {
                MessageBox.Show("Los SKUs sólo pueden ser asociados a una subcategoría");
            }
            else
            {
                if (treeview != null && treeview.SelectedItem != null)
                {
                    if (sourceCategoryItem != null)
                    {
                        ((Article)sourceCategoryItem).Category = ((Category)treeview.SelectedItem);
                        ((Article)sourceCategoryItem).CategoryId = ((Category)treeview.SelectedItem).Id;
                        if (sourceCategoryRadDropDownButton != null)
                        {
                            sourceCategoryRadDropDownButton.IsOpen = false;
                            this.ArticlesGV.CommitEdit();
                        }
                    }
                }
            }
        }



        private void HeaderCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            string column = ((CheckBox)sender).Name;
            foreach (var item in this.ArticlesGV.Items)
            {
                Article article = item as Article;
                switch (column)
                {
                    case "IsDelisted":
                        article.IsDelisted = true;
                        break;
                    case "IsNew":
                        article.IsNew = true;
                        break;
                }
            }

            this.ArticlesGV.Rebind();
        }

        private void HeaderCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            string column = ((CheckBox)sender).Name;
            foreach (var item in this.ArticlesGV.Items)
            {
                Article article = item as Article;
                switch (column)
                {
                    case "IsDelisted":
                        article.IsDelisted = false;
                        break;
                    case "IsNew":
                        article.IsNew = false;
                        break;
                }
            }

            this.ArticlesGV.Rebind();
        }
    }



}
